package com.guoxin.artconcurrency.chapter3;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author guoxin
 * @date 2019/8/7
 */
public class UnsafelyVolatile {
    private static volatile AtomicInteger num = new AtomicInteger();
    private static final int THREAD_COUNT = 20;
    private static final CountDownLatch LATCH = new CountDownLatch(THREAD_COUNT);

    private static void increaseNum() {
        num.incrementAndGet();
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < THREAD_COUNT; i++) {
            new Thread(() -> {
                for (int j = 0; j < 10000; j++) {
                    increaseNum();
                }
                LATCH.countDown();
            }).start();
        }

        LATCH.await();
        System.out.println(num);
    }
}
